package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanServce;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class NursingPlanServiceimpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanServce nursingProjectPlanServce;

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> queryPlan() {
        List<NursingPlanVo> vo = nursingPlanMapper.selectPlan();
        return vo;
    }

    /**
     * 分页查询
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> selectPlan(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> nursingPlanVos = nursingPlanMapper.PaginationQuery(name, status);

        return PageResponse.of(nursingPlanVos, NursingPlanVo.class);
    }

    /**
     * 新增护理计划
     *
     * @param dto
     */
    @Override
    @Transactional
    public void addPlan(NursingPlanDto dto) {
        Integer count = nursingPlanMapper.nursingPlanName(dto.getPlanName());
        if (count > 0) {
            throw new BaseException(BasicEnum.DELETE_XIANGMUJIHUANAMECHONGFU);
        }

        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setStatus(1);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlanMapper.insert(nursingPlan);
        if (!CollectionUtils.isEmpty(nursingPlan.getProjectPlans())) {
            nursingPlan.getProjectPlans().forEach(e -> {
                e.setPlanId(nursingPlan.getId());
            });

            nursingProjectPlanServce.addProjectPlan(nursingPlan.getProjectPlans());
        }

    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo selectNursingPlan(Integer id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);

        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        return vo;
    }

    /**
     * 修改护理计划
     *
     * @param dto
     */
    @Override
    @Transactional
    public void modifyNursingPlan(NursingPlanDto dto) {

        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlanMapper.update(nursingPlan);


        nursingProjectPlanServce.deleteNursingProjectPlan(nursingPlan.getId());

        if (!CollectionUtils.isEmpty(nursingPlan.getProjectPlans())) {
            nursingPlan.getProjectPlans().forEach(e -> {
                e.setPlanId(nursingPlan.getId());
            });

            nursingProjectPlanServce.addProjectPlan(nursingPlan.getProjectPlans());
        }
    }

    /**
     * 删除护理计划
     * @param id
     */
    @Transactional
    @Override
    public void deleteNursingPlan(Integer id) {
        nursingPlanMapper.deleteById(id);
        nursingProjectPlanServce.deletePlan(id);
    }

    /**
     * 护理计划的启用禁用
     * @param id
     * @param status
     */
    @Override
    public void EnableDisableNursingPlan(Integer id, Integer status) {
        nursingPlanMapper.EnableDisableNursingPlan(id, status);
    }

}
